﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FlowersStore
{
	public partial class CustomerData : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
		    if (IsPostBack) return;
		   // ((FlowersStore.SiteMaster)this.Master).FillCartItems();

		    if (Session["order"] == null)
		        Response.Redirect("Cart.aspx");
		    else
		    {
		        UserOrder order = (UserOrder)Session["order"];
		        TryFillFields(order);
		        lblPrice.Text = order.TotalPrice.ToString("n0");
		    }
		    cDeliveryDate.SelectedDate = DateTime.Today.AddDays(1);
		}

		private void TryFillFields(UserOrder order)
		{			
			tbReceiverName.Text = order.receiverName;
			tbReceiverPhones.Text = order.receiverPhone;
			tbAdditional.Text = order.receiverAddInfo;
			tbAdditionalSender.Text = order.senderAddInfo;
			tbSenderName.Text = order.senderName;
			tbSenderPhones.Text = order.senderPhone;
			tbWishes.Text = order.addInfo;
			if (order.deliveryDate > DateTime.Today && order.deliveryDate <= DateTime.Today.AddDays(7))
				cDeliveryDate.SelectedDate = order.deliveryDate;
			tbReceiverAddress.Text = order.address;
		}

		protected void ValidateDeliveryDate(object sender, ServerValidateEventArgs args)
		{
			args.IsValid = cDeliveryDate.SelectedDate <= DateTime.Today || cDeliveryDate.SelectedDate > DateTime.Today.AddDays(7);
			
		}

		protected void DateChanged(object sender, EventArgs e)
		{
			if (cDeliveryDate.SelectedDate <= DateTime.Today || cDeliveryDate.SelectedDate > DateTime.Today.AddDays(7))
			{
				string msg = "alert('Доставка возможна только в течение недели с завтрашнего дня')";
				ScriptManager.RegisterStartupScript(this, this.GetType(), "UserPopup", msg, true);
			}
		}

		protected void btnContinue_Click(object sender, EventArgs e)
		{
			if (cDeliveryDate.SelectedDate <= DateTime.Today || cDeliveryDate.SelectedDate > DateTime.Today.AddDays(7))
			{
				string msg = "alert('Доставка возможна только в течение недели с завтрашнего дня')";
				ScriptManager.RegisterStartupScript(this, this.GetType(), "UserPopup", msg, true);
				return;
			}
			else
			{
				UserOrder order = (UserOrder)Session["order"];
				order.receiverName = tbReceiverName.Text;
				order.receiverPhone = tbReceiverPhones.Text;
				order.receiverAddInfo = tbAdditional.Text;
				order.senderAddInfo = tbAdditionalSender.Text;
				order.senderName = tbSenderName.Text;
				order.senderPhone = tbSenderPhones.Text;
				order.addInfo = tbWishes.Text;
				order.deliveryDate = cDeliveryDate.SelectedDate;
				order.address = tbReceiverAddress.Text;
				switch (ddlTime.SelectedValue)
				{
 					case "0":
						order.startTime = order.deliveryDate.AddHours(10);
						order.endTime = order.deliveryDate.AddHours(20);
						break;
					case "1":
						order.startTime = order.deliveryDate.AddHours(8);
						order.endTime = order.deliveryDate.AddHours(10);
						break;
					case "2":
						order.startTime = order.deliveryDate.AddHours(10);
						order.endTime = order.deliveryDate.AddHours(12);
						break;
					case "3":
						order.startTime = order.deliveryDate.AddHours(12);
						order.endTime = order.deliveryDate.AddHours(14);
						break;
					case "4":
						order.startTime = order.deliveryDate.AddHours(14);
						order.endTime = order.deliveryDate.AddHours(16);
						break;
					case "5":
						order.startTime = order.deliveryDate.AddHours(16);
						order.endTime = order.deliveryDate.AddHours(18);
						break;
					case "6":
						order.startTime = order.deliveryDate.AddHours(18);
						order.endTime = order.deliveryDate.AddHours(20);
						break;
					case "7":
						order.startTime = order.deliveryDate.AddHours(20);
						order.endTime = order.deliveryDate.AddHours(22);
						break;
				}
				Session["order"] = order;
				Response.Redirect("OrderSummary.aspx");
			}
		}
	}
}